########## MAIN ##########

# Espace de travail
#setwd("Z:/4info/projet_tdmm/Kredit")
#setwd("C:/Users/Max/Desktop/tdmm_project/FichiersDonnees")
#setwd("C:/Users/Aurelien/Desktop/tdmm_project/FichiersDonnees")
#setwd("Z:/ProjetKredit/FichiersDonnees")
#setwd("~/tdmm/FichiersDonnees")
source("../CodeR/script-fcm.R") 
source("../CodeR/diversite.R") 
source("../CodeR/script-rcm.R") 
source("../CodeR/rcm-min.R") 
source("../CodeR/rcm-max.R") 
source("../CodeR/rcm-mean.R") 
source("../CodeR/rcm-trim-mean.R") 
source("../CodeR/rcm-med.R") 
source("../CodeR/rcm-prod.R")
source("../CodeR/diversite.R")
source("../CodeR/profil-moyen.R")

########## FCM ##########

# Importation des donnees
kredit <-  read.table("kredit.txt", header=TRUE,  sep="", na.strings="NA", dec=".", strip.white=TRUE)
#summary(kredit)
var <-  sample(c(1:1000), 1000)
kredit_learning <- kredit[var[1:700],]
kredit_validation <-  kredit[var[701:1000],]

vecteur_scores =  matrix(-1, nrow(kredit_validation), 2)
vecteur_scores_fcm = Favorite_Class_Model(kredit_learning, kredit_validation)



########## RCM ##########
vecteur_scores_rcm = Random_Choice_Model(0.11,kredit_learning, kredit_validation)

# Definition du seuil de correlation pour la selection
prediction_max = RCM_max (vecteur_scores_rcm, kredit_validation)
prediction_min = RCM_min (vecteur_scores_rcm, kredit_validation)
prediction_mean = RCM_mean (vecteur_scores_rcm, kredit_validation) 
prediction_trim_mean = RCM_trim_mean (vecteur_scores_rcm, kredit_validation) 
prediction_med = RCM_med (vecteur_scores_rcm, kredit_validation) 
prediction_prod = RCM_prod (vecteur_scores_rcm, kredit_validation) 
prediction_profil_moyen = RCM_profil_moyen (vecteur_scores_rcm, kredit_validation)

the_matrix = matrix(-1,nrow(kredit_validation),8)
the_matrix[,1]=prediction_min
the_matrix[,2]=prediction_max
the_matrix[,3]=prediction_mean
the_matrix[,4]=prediction_trim_mean
the_matrix[,5]=prediction_med
the_matrix[,6]=prediction_prod
for(i in 1:nrow(kredit_validation)){
	the_matrix[i,7] <- mean(c(the_matrix[i,1], the_matrix[i,2], the_matrix[i,3], the_matrix[i,4], the_matrix[i,5], the_matrix[i,6]))
}
the_matrix[,8] = kredit[row.names(matrice_scores),1]
row.names(the_matrix) = row.names(matrice_scores)
colnames(the_matrix)=c("pred_min","pred_max","pred_moy", "pred_trim_mean", "pred_prod", "pred_median", "moyenne", "kredit")
print(the_matrix)

for(i in 1:nrow(select_classif)){
	matrice_score[i,] <- vecteur_score[,select_classif[i]]
}


nb_erreur = xor(the_matrix[,7], the_matrix[,8])
proportion_erreur = sum(nb_erreur) / nrow(the_matrix)
print(paste("proportion d'erreur", proportion_erreur, sep=" : "))

